package org.yf.samples.util;

import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.NotOLE2FileException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.yf.samples.model.ExamUserVO;

public class ExcelUtils {

	public static String OFFICE_EXCEL_2007_POSTFIX = ".xlsx";
	public static String OFFICE_EXCEL_2003_POSTFIX = ".xls";
	
	public static Workbook openWorkbook(InputStream is, String postfix){
		Workbook hssfWorkbook = null;
		try{
			if(OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){
				hssfWorkbook = new HSSFWorkbook(is);
			}else if(OFFICE_EXCEL_2007_POSTFIX.equals(postfix)){
				hssfWorkbook = new XSSFWorkbook(is);
			}
		}catch(Exception e){
			if(e instanceof NotOLE2FileException){
				return null;
			}
			e.printStackTrace();
		}
		return hssfWorkbook;
	}

	public static List<ExamUserVO> getStudentListByExcel(Workbook wk) {
		final String GENDER_MALE_STRING = "男", GENDER_FEMALE_STRING = "女";
		final String GENDER_MALE_INTEGER = "0", GENDER_FEMALE_INTEGER = "1";
		List<ExamUserVO> resultList = new ArrayList<ExamUserVO>();
		Sheet sheet = wk.getSheetAt(0);
		System.out.println(wk);
		int maxCellNubmer = 0;
		int maxRowNumber = sheet.getLastRowNum();
		ExamUserVO vo = null;
		for(int currentRowNumber = 1; currentRowNumber <= maxRowNumber; currentRowNumber++){
			Row row = sheet.getRow(currentRowNumber);
			maxCellNubmer = row.getLastCellNum();
			vo = new ExamUserVO();
			for(int currentCellRowNumber = 0; currentCellRowNumber < maxCellNubmer; currentCellRowNumber++){
				Cell cell = row.getCell(currentCellRowNumber);
				switch(currentCellRowNumber){
					case 0:
						vo.setUserId(getCellValue(cell));
						break;
					case 1:
						vo.setUserName(getCellValue(cell));
						break;
					case 2:
						String gender = getCellValue(cell);
						if(GENDER_MALE_STRING.equals(gender)){
							vo.setGender(GENDER_MALE_INTEGER);
						}else if(GENDER_FEMALE_STRING.equals(gender)){
							vo.setGender(GENDER_FEMALE_INTEGER);
						}
						break;
					case 3:
						vo.setTel(getCellValue(cell));
						break;
					case 4:
						vo.setEmail(getCellValue(cell));
						break;
					case 5:
						vo.setAddress(getCellValue(cell));
						break;
					case 6:
						vo.setBirthday(getCellValue(cell));
						break;
					case 7:
						String decodingPassWord = getCellValue(cell);
						vo.setPassword(MD5Utils.GetMd5Code(decodingPassWord));
						break;
				}
			}
			resultList.add(vo);
		}
		return resultList;
	}
	
	@SuppressWarnings("static-access")
	private static String getCellValue(Cell row){
		if(row == null){
			return "";
		}
		if(row.getCellType() == row.CELL_TYPE_BOOLEAN){
			return String.valueOf(row.getBooleanCellValue());
		}else if(row.getCellType() == row.CELL_TYPE_NUMERIC){
			DecimalFormat df = new DecimalFormat("0");
		    if(HSSFDateUtil.isCellDateFormatted(row)){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
			    String  str = sdf.format(HSSFDateUtil.getJavaDate(row.getNumericCellValue()));
			    return str; 
		    }else{
		    	return df.format(row.getNumericCellValue());
		    }
		}else{
			return String.valueOf(row.getStringCellValue());
		}
	}
}
